Python Program to Find Length of a String
Q. Write a Python program to take user input and calculate length of a string.
Use built in len() Python function to calculate the length of a string.
This function take one argument of type object. If the input object is a string, this function will calculate length of a string.
Example:
1 2 3 4 5 6 | #Python program to calculate length of a string x = "Tecadmin" n = len(x) print(n) |
Output
8
Example with User Input
Another Python program to take input of string from user and calculate the length.
1 2 3 4 5 6 | #Python program to take user input and calculate string length x = input("Enter a string: ") n = len(x) print(n) |
Execute above Python script. enter a string when prompt and program will print the length of string.